﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Formatters.Soap;
using System.Reflection;
using System.IO;
using System.Xml.Serialization;
using Toodledo.Model;

namespace Sync
{
    public static class SerializeStatic
    {

        private static readonly IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForAssembly();
        private static string _filename = "lastsession.xml";

        public static int Test1 = 0;
        public static int Test { get; set; }
        public static List<string> stringlist;
        public static List<Folder> folders;

        public static bool Save(Type staticClass, string filename)
        {
            try
            {
                FieldInfo[] fields = staticClass.GetFields(BindingFlags.Static | BindingFlags.Public);
                object[,] a = new object[fields.Length, 2];
                int i = 0;
                foreach (FieldInfo field in fields)
                {
                    a[i, 0] = field.Name;
                    a[i, 1] = field.GetValue(null);
                    i++;
                };

                IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_filename, FileMode.Create, Store);

                //Stream f = File.Open(filename, FileMode.Create);
                XmlSerializer serializer  = new XmlSerializer(typeof(object[,]));
                serializer.Serialize(stream, a);
                stream.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Load(Type staticClass, string filename)
        {
            try
            {
                FieldInfo[] fields = staticClass.GetFields(BindingFlags.Static | BindingFlags.Public|BindingFlags.GetProperty);
                object[,] a;
                IsolatedStorageFileStream stream = null;
                if (Store.FileExists(_filename))
                {
                    stream = new IsolatedStorageFileStream(_filename, FileMode.Open, Store);
                    SoapFormatter formatter = new SoapFormatter();
                    a = formatter.Deserialize(stream) as object[,];
                    stream.Close();
                    if (a.GetLength(0) != fields.Length) return false;
                    int i = 0;
                    foreach (FieldInfo field in fields)
                    {
                        if (field.Name == (a[i, 0] as string))
                        {
                            field.SetValue(null, a[i, 1]);
                        }
                        i++;
                    }
                    ;
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
            return true;
        }
    }
}
